package com.luban.fox.security01.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author Fox
 */
@Service
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //1.非加密方式：
        User fox = new User("fox", "{noop}123456", AuthorityUtils.commaSeparatedStringToAuthorityList("admin,user"));
//
        //自定义登录逻辑   查mysql
        //2.使用bcrypt加密方式：
        //fox = new User("fox", "{bcrypt}"+BCrypt.hashpw("123456",BCrypt.gensalt()), AuthorityUtils.commaSeparatedStringToAuthorityList("admin,user"));

        //3.使用password加密方式：
        String pw = passwordEncoder.encode("123456");
        fox = new User("fox", pw, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,user"));
        return fox;
    }
}
